﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using Water.Web.Controls.BaseInterface;
using Water.Web.JSON;
using System.Web.UI.Design;
using System.Drawing.Design;
using System.Drawing;

[assembly: WebResource("Water.Resources.Default.JS.Dialog.Dialog.js", "text/javascript")]
namespace Water.Web.Controls
{
    [ToolboxData("<{0}:SearchBox runat=server></{0}:SearchBox>")]
    public class SearchBox : BaseTextBox,INamingContainer
    {
         private Hashtable ht = new Hashtable();
         private System.Web.UI.WebControls.HiddenField input_value;

         protected override void OnInit(EventArgs e)
         {
             base.OnInit(e);
             this.TokenCssClass = "field_token_search";
         }

         protected override void CreateChildControls()
         {
             input_value = new HiddenField();
             this.input_value.ID = "value";
             this.Controls.Add(input_value);
         }

         public override void RenderBeginTag(HtmlTextWriter writer)
        {
            base.RenderBeginTag(writer);
            this.input_value.RenderControl(writer);
        }

         protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Page.ClientScript.RegisterClientScriptResource(typeof(Dialog), "Water.Resources.Default.JS.Dialog.Dialog.js");
        }

         public override string QueryProperty
        {
            get
            {
                return this.input_value.Value;
            }
        }

         [Bindable(true, BindingDirection.TwoWay), Localizable(true), Category("Appearance"), DefaultValue(""), GlobalizeDescription("Value")]
         public override string Value
         {
             get
             {
                 return this.input_value.Value;
             }
             set
             {
                 EnsureChildControls();
                 this.input_value.Value = value;
             }
         }

        private string dialogId;

        [Browsable(true), GlobalizeDescription("DialogId"), Category("Behavior")]
        public string DialogId
        {
            get { return dialogId; }
            set { dialogId = value; }
        }
        private string src;

        [Browsable(true), GlobalizeDescription("Src"), Category("Behavior"),Editor("System.Web.UI.Design.UrlEditor",typeof(UITypeEditor))]
        public string Src
        {
            get { return src; }
            set { src = value; }
        }

        private int dialogWidth;
        [GlobalizeDescription("DialogWidth"), Category("Layout")]
        public virtual int DialogWidth
        {
            get
            {
                return this.dialogWidth;
            }
            set
            {
                this.dialogWidth = value;
                JSONSettingUtil.SetHashtable(ht, "width", value.ToString());
            }
        }

        private int dialogHeight;
        [GlobalizeDescription("DialogHeight"), Category("Layout")]
        public virtual int DialogHeight
        {
            get
            {
                return this.dialogHeight;
            }
            set
            {
                this.dialogHeight = value;
                JSONSettingUtil.SetHashtable(ht, "height", value);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            string js;
            if (string.IsNullOrEmpty(this.Src) && !string.IsNullOrEmpty(this.DialogId))
            {
                js = @"$j(function() {$j('#" + this.ClientID + "_token').click(function(){$j('#" +Util.Utils.FindControlEx(this.DialogId,this.Page).ClientID + "').showDialog('" + this.ClientID + "_token',1);});});";
                this.Page.ClientScript.RegisterStartupScript(typeof(SearchBox), this.UniqueID, js, true);
            }
            else if (!string.IsNullOrEmpty(this.Src))
            {
                js = @"$j(function() {$j('#" + this.ClientID + "_token').click(function(){$j(this).open('" + this.Src + "'," + toKeyValue() +",'" + this.input_value.ClientID + "','" + this.ClientID + "',1);});});";
                this.Page.ClientScript.RegisterStartupScript(typeof(SearchBox), this.UniqueID, js, true);
            }
            base.OnPreRender(e);

        }

        private string toKeyValue()
        {
            string strReturn = "";
            strReturn = JSONSettingUtil.ToKeyValue(ht);
            return "{" + strReturn + "}";
        }
    }
}
